package com.jzo2o.foundations.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.DBException;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.foundations.enums.FoundationStatusEnum;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.model.domain.Serve;
import com.jzo2o.foundations.model.domain.ServeItem;
import com.jzo2o.foundations.model.dto.request.ServePageQueryReqDTO;
import com.jzo2o.foundations.model.dto.request.ServeUpsertReqDTO;
import com.jzo2o.foundations.model.dto.response.ServeResDTO;
import com.jzo2o.foundations.service.IRegionService;
import com.jzo2o.foundations.service.IServeItemService;
import com.jzo2o.foundations.service.IServeService;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 服务表 服务实现类
 * </p>
 *
 * @author yiWen
 * @since 2025-01-16
 */
@Service
public class ServeServiceImpl extends ServiceImpl<ServeMapper, Serve> implements IServeService {

    @Override
    public PageResult<ServeResDTO> pageList(ServePageQueryReqDTO dto) {
        return PageHelperUtils.selectPage(dto,
                () -> baseMapper.queryList(dto.getRegionId()));

    }

    @Autowired
    private IServeItemService serveItemService;
    @Autowired
    private IRegionService regionService;

    @Override
    @Transactional
    public void batchAdd(List<ServeUpsertReqDTO> dtos) {
        for (ServeUpsertReqDTO dto : dtos) {
            //1.判断服务相是否存在
            ServeItem byId = serveItemService.getById(dto.getServeItemId());
            if (ObjectUtil.isEmpty(byId)) {
                throw new ForbiddenOperationException("服务项不存在");
            }
            //2.判断服务相是否启动
            if (byId.getActiveStatus() != FoundationStatusEnum.ENABLE.getStatus()) {
                throw new ForbiddenOperationException("服务项未启用");

            }
            //3.判断服务相是否添加过
            Integer count = this.lambdaQuery().eq(Serve::getRegionId, dto.getRegionId())
                    .eq(Serve::getServeItemId, dto.getServeItemId())
                    .count();
            if (count > 0) {
                throw new ForbiddenOperationException("服务项重复");
            }
            //4.查询区域信息
            String cityCode = regionService.getById(dto.getRegionId()).getCityCode();

            //5.复制服务项味服务信息
            Serve serve = BeanUtils.copyBean(dto, Serve.class);
            serve.setCityCode(cityCode);


            //6.新增区域服务信息
            boolean save = this.save(serve);
            if (!save) {
                throw new DBException("新增失败");
            }

        }

    }

    @Override
    public void updatePrice(Long id, BigDecimal price) {
        //1.判断服务是否失败
        Serve serve = this.getById(id);
        if (ObjectUtil.isEmpty(serve)) {
            throw new ForbiddenOperationException("服务不存在");
        }
        //2.更新服务价格
        boolean update = this.lambdaUpdate().set(Serve::getPrice, price).eq(Serve::getId, id).update();
        if (!update) {
            throw new DBException("更新失败");
        }
    }

    @Override
    public void onsale(Long id) {
        //1.判断服务是否存在
        Serve serve = this.getById(id);
        if (ObjectUtil.isEmpty(serve)) {
            throw new ForbiddenOperationException("服务不存在");
        }
        //2.判断是否有区域
        if(serve.getRegionId()==null){
            throw new ForbiddenOperationException("服务未关联区域");
        }
        //3.判断服务是否上架过
        if (serve.getSaleStatus() == FoundationStatusEnum.ENABLE.getStatus()) {
            throw new ForbiddenOperationException("服务已上架");
        }
        //4.判断服务项是否存在
        ServeItem serveItem = serveItemService.getById(serve.getServeItemId());
        if (ObjectUtil.isEmpty(serveItem)) {
            throw new ForbiddenOperationException("服务项不存在");
        }

        //5.判断服务项是否已启动
        if (serveItem.getActiveStatus() != FoundationStatusEnum.ENABLE.getStatus()) {
            throw new ForbiddenOperationException("服务项未启用");
        }
        //6.修改状态为已上架
        boolean update = this.lambdaUpdate()
                .eq(Serve::getId,id)
                .set(Serve::getSaleStatus, FoundationStatusEnum.ENABLE.getStatus())
                .update();
        if (!update) {
            throw new DBException("更新失败");
        }

    }

    @Override
    @Transactional
    public void offsale(Long id) {
        //1.判断服务是否存在
        Serve serve = this.getById(id);
        if (ObjectUtil.isEmpty(serve)) {
            throw new ForbiddenOperationException("服务不存在");
        }
        //2.判断是否有区域
        if(serve.getRegionId()==null){
            throw new ForbiddenOperationException("服务未关联区域");
        }
        //3.判断服务是否下架
        if (serve.getSaleStatus() == FoundationStatusEnum.DISABLE.getStatus()) {
            throw new ForbiddenOperationException("服务已下架");
        }

        //6.修改状态为已下架
        boolean update = this.lambdaUpdate()
                .eq(Serve::getId,id)
                .set(Serve::getSaleStatus, FoundationStatusEnum.DISABLE.getStatus())
                .update();
        if (!update) {
            throw new DBException("更新失败");
        }
    }

    @Override
    public void deleteById(Long id) {
        Serve serve = this.getById(id);
        if(serve==null){
            throw new ForbiddenOperationException("服务不存在");

        }
        if (serve.getSaleStatus() == FoundationStatusEnum.ENABLE.getStatus()) {
            throw new ForbiddenOperationException("服务已上架");
        }
        boolean b = this.removeById(id);
        if (!b) {
            throw new DBException("删除失败");
        }

    }

    @Override
    @Transactional
    public void onHot(Long id) {
        Serve serve = this.getById(id);
        if (serve == null) {
            throw new ForbiddenOperationException("服务不存在");
        }
        boolean update = this.lambdaUpdate().eq(Serve::getId, id).set(Serve::getIsHot, 1).update();
        if (!update) {
            throw new DBException("更新失败");
        }

    }

    @Override
    @Transactional
    public void offHot(Long id) {
        Serve serve = this.getById(id);
        if (serve == null) {
            throw new ForbiddenOperationException("服务不存在");
        }
        boolean update = this.lambdaUpdate().eq(Serve::getId, id).set(Serve::getIsHot, 0).update();
        if (!update) {
            throw new DBException("更新失败");
        }

    }
}
